<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :before-close="closeDialog"
    :close-on-click-modal="false"
    width="30%"
  >
    <el-form
      ref="createOrEditRoleForm"
      v-loading="loading"
      label-position="top"
      :model="form"
      :rules="formRules"
    >
      <el-form-item :label="$t('AbpTenantManagement[\'RoleName\']')" prop="name">
        <el-input ref="nameInput" v-model="form.name" />
      </el-form-item>
      <el-form-item prop="isDefault">
        <el-checkbox
          v-model="form.isDefault"
          :label="$t('AbpTenantManagement[\'DisplayName:IsDefault\']')"
          name="type"
        />
      </el-form-item>
      <el-form-item prop="isPublic">
        <el-checkbox
          v-model="form.isPublic"
          :label="$t('AbpTenantManagement[\'DisplayName:IsPublic\']')"
          name="type"
        />
      </el-form-item>
      <!-- 隐藏表单 -->
      <el-form-item prop="id" hidden>
        <el-input v-model="form.id" />
      </el-form-item>
      <el-form-item prop="concurrencyStamp" hidden>
        <el-input v-model="form.concurrencyStamp" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">{{ $t('AbpTenantManagement[\'Cancel\']') }}</el-button>
      <el-button type="primary" @click="submitForm">
        <i class="el-icon-check" />
        {{ $t('AbpTenantManagement[\'Save\']') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  getById,
  create,
  edit
} from '@/api/administration/role/role'
export default {
  name: 'CreateOrEditRole',
  props: {
    context: {
      type: Object,
      required: true
    },
    visible: {
      type: Boolean,
      required: true,
      default: false
    }
  },
  data() {
    return {
      title: '',
      actionMode: '',
      loading: false,
      form: {
        id: '',
        name: '',
        concurrencyStamp: '',
        isDefault: false,
        isPublic: false
      },
      formRules: {
        name: [{ required: true, message: this.$t('AbpTenantManagement[\'Required\']'), trigger: 'blur' }]
      }
    }
  },
  watch: {
    context(newVal, oldVal) {
      this.loading = true
      this.$nextTick(function() {
        this.title = newVal.title
        this.actionMode = newVal.actionMode
        if (this.actionMode === 'edit') {
          /* 编辑模式 */
          if (newVal.id) {
            getById(newVal.id).then(resp => {
              const role = resp
              if (role) {
                this.form.id = newVal.id
                this.form.name = role.name
                this.form.concurrencyStamp = role.concurrencyStamp
                this.form.isDefault = role.isDefault
                this.form.isPublic = role.isPublic
              }
              this.loading = false
            })
          }
        } else {
          /* 创建模式 */
          this.loading = false
        }
      })
    }
  },
  methods: {
    submitForm() {
      this.$refs.createOrEditRoleForm.validate(async(isValid, invalidFields) => {
        if (isValid) {
          if (this.actionMode === 'edit') {
            await edit(this.form.id, this.form)
          } else {
            await create(this.form)
          }
          this.$message({ message: this.$t('AbpTenantManagement[\'Ok\']'), type: 'success' })
          this.closeDialog()
          this.$emit('saveSucceed')
        }
      })
    },
    closeDialog() {
      this.resetDialog() // 重置表单
      this.$emit('update:visible', false)
    },
    resetDialog() {
      this.$refs.createOrEditRoleForm.resetFields()
    }
  }
}
</script>

<style scoped>
</style>
